﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Shine.Util.IO;

namespace Shine.Util.Log
{
    /// <summary>
    /// 日志操作类
    /// </summary>
    public class LogHelper
    {
        #region 文本日志
        /// <summary>
        /// 文本日志文件存储的目录
        /// </summary>
        private static string _DirLog = "";

        /// <summary>
        /// 获取或设置文本日志文件存储的目录。
        /// 默认为系统运行目录的Log目录下，若要更改，则在系统启动时修改。
        /// </summary>
        public static string DirLog
        {
            get
            {
                if (string.IsNullOrEmpty(_DirLog))
                {
                    _DirLog = PathHelper.GetAppPath() + "\\Log";
                }
                return _DirLog;
            }
            set
            {
                _DirLog = value;
            }
        }
        
        /// <summary>
        /// 错误日志文件全路径
        /// </summary>
        private static string DirError
        {
            get { return DirLog + @"\Error" + DateTime.Now.ToString("yyyy-MM-dd") + ".log"; }
        }
        
        /// <summary>
        /// 操作日志文件全路径
        /// </summary>
        private static string DirDone
        {
            get { return DirLog + @"\Done" + DateTime.Now.ToString("yyyy-MM-dd") + ".log"; }
        }
        
        /// <summary>
        /// 普通日志文件全路径
        /// </summary>
        private static string DirNormal
        {
            get { return DirLog + @"\Normal" + DateTime.Now.ToString("yyyy-MM-dd") + ".log"; }
        }
        
        /// <summary>
        /// 记录错误日志信息到文本文件中
        /// </summary>
        /// <param name="ex">异常消息</param>
        public static void WriteErrorText(Exception ex)
        {
            //string msg = "异常消息：" + ex.Message + "。";
            //msg += "异常类型：" + ex.GetType().ToString() + "。";
            //msg += "对象名称:" + ex.Source + "。";
            //if (ex.TargetSite!=null)
            //    msg += "引发当前异常的方法:" + ex.TargetSite.Name + "。";
            //WriteErrorText(msg);
            WriteErrorText(ex.ToString());
        }
        
        /// <summary>
        /// 记录错误日志到文本文件中
        /// </summary>
        /// <param name="message">要记录的消息</param>
        public static void WriteErrorText(string message)
        {
            Write(DirError, message);
        }
        
        /// <summary>
        /// 记录操作日志到文本文件中
        /// </summary>
        /// <param name="message">要记录的消息</param>
        public static void WriteDoneText(string message)
        {
            Write(DirDone, message);
        }
        
        /// <summary>
        /// 记录警告日志到文本文件中
        /// </summary>
        /// <param name="message">要记录的消息</param>
        public static void WriteNormalText(string message)
        {
            Write(DirNormal, message);
        }
        
        /// <summary>
        /// 记录日志到文本中
        /// </summary>
        /// <param name="fullname">文本文件全路径</param>
        /// <param name="message">要记录的消息</param>
        private static void Write(string fullname, string message)
        {
            if (string.IsNullOrEmpty(DirLog) || string.IsNullOrEmpty(fullname))
                return;
            if (!Directory.Exists(DirLog))
                Directory.CreateDirectory(DirLog);
            StreamWriter sw = null;
            try
            {
                if (File.Exists(fullname))
                    sw = File.AppendText(fullname);
                else
                    sw = File.CreateText(fullname);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + message);
                sw.Flush();
            }
            catch
            {

            }
            finally
            {
                if (sw != null)
                    sw.Close();
                sw = null;
            }
        }

        #endregion

        #region 自定义日志

        /// <summary>
        /// 记录日志的操作委托，使用此对象，需要在系统运行时赋值（值为自定义的一个方法，用来记录日志）
        /// </summary>
        public static WriteLog2 WriteLogEvent { get; set; }

        /// <summary>
        /// 记录日志信息
        /// </summary>
        /// <param name="log">日志消息，值为 字符串、LogMessage对象、Exception对象等</param>
        public static void WriteLog(dynamic log)
        {
            if (WriteLogEvent != null)
                WriteLogEvent(log);
            else
            {
                if (log is string)
                {
                    WriteDoneText((string)log);
                }
                else if (log is LogMessage)
                {
                    LogMessage m = (LogMessage)log;
                    if (m.LogType == zjgisLogType.zjgisLogError)
                    {
                        WriteErrorText(m.Message);
                    }
                    else
                    {
                        WriteDoneText(m.Message);
                    }
                }
                else if (log is Exception)
                {
                    WriteErrorText((Exception)log);
                }
            }
        }

        /// <summary>
        /// 记录日志信息
        /// </summary>
        /// <param name="msg">日志消息</param>
        /// <param name="logtype">消息类型</param>
        public static void WriteLog(string msg, zjgisLogType logtype)
        {
            WriteLog(new LogMessage()
            {
                Message = msg,
                LogType = logtype
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        public static void WriteLog(string msg)
        {
            WriteLog(new LogMessage()
            {
                Message = msg,
                LogType = zjgisLogType.zjgisLogNormal
            });
        }

        /// <summary>
        /// 记录日志信息
        /// </summary>
        /// <param name="msg">日志消息</param>
        public static void WriteErrLog(string msg)
        {
            WriteLog(msg, zjgisLogType.zjgisLogError);
        }

        /// <summary>
        /// 记录日志信息
        /// </summary>
        /// <param name="msg">日志消息</param>
        public static void WriteDoneLog(string msg)
        {
            WriteLog(msg, zjgisLogType.zjgisLogDone);
        }
        #endregion

        

    }
}
